package presentation.concurrency;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * User: mvillalobos
 * Date: 7/10/12
 * Time: 8:42 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProducerConsumer {

	private final BlockingQueue<AtomicInteger> queue = new ArrayBlockingQueue<AtomicInteger>(100);

	public static void main(String args[]) {

		final BlockingQueue<AtomicInteger> queue = new ArrayBlockingQueue<AtomicInteger>(100);
		ExecutorService executorService = Executors.newFixedThreadPool(20);
		executorService.submit(new Runnable() {
			@Override
			public void run() {

				try {
					queue.put(new AtomicInteger(new Random().nextInt()));
				} catch (InterruptedException e) {
					e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
				}
			}
		});

		executorService.submit(new Runnable() {
			@Override
			public void run() {
				try {
					AtomicInteger integer = queue.take();
				} catch (InterruptedException e) {
					e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
				}
			}
		});
	}
}
